数据结构 – 数组
1.数组结构
- JavaScript中创建和初始化数组:
1 | var daysOfWeek = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', |
- 获取数组长度
1 | alert(daysOfweek.length) |
// 普通for方式遍历数组 for (var i = 0; i < daysOfWeek.length; i++) { alert(daysOfWeek[i]) } // 通过foreach遍历数组 daysOfWeek.forEach(function (value) { alert(value) }) <!--hexoPostRenderEscape:<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">* 数组的增删改查</span><br><span class="line"></span><br><span class="line">```javascript</span><br><span class="line">// 添加一个元素到数组的最后位置</span><br><span class="line">// 方式一:</span><br><span class="line">numbers[numbers.length] = 10</span><br><span class="line"></span><br><span class="line">// 方式二:</span><br><span class="line">numbers.push(11)</span><br><span class="line">numbers.push(12, 13)</span><br><span class="line"></span><br><span class="line">alert(numbers)</span><br></pre></td></tr></table></figure>:hexoPostRenderEscape-->
1 | // 在数组首位插入一个元素 |
1 | // 删除最后的元素 |
通过splice删除数据
1 | // 删除指定位置的几个元素 |
使用splice来插入数据
1 | // 插入指定位置元素 |
使用splice修改数据
1 | // 修改指定位置的元素 |
splice(起始位置[,删除元素个数[,插入的元素[,插入的第N个元素]]])
1 | // 数组的合并 |
迭代方法
every()
判断一组元素中是否都包含某一个字符
let name=['abc','adc','amn','azq'] let f = name.every(function(t){ return t.indexOf('a')!=-1 }) console.log(f) <!--hexoPostRenderEscape:<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">* filter()方法</span><br><span class="line"></span><br><span class="line"> * 是一种过滤函数 遍历数组中每一个元素 传入到这个函数中, 如果返回true 那么找到的这个元素回被添加到最新的数组中 若返回false 那么忽略 </span><br><span class="line"></span><br><span class="line"> * 最后会创建一个新的数组 新的数组中的元素是通过检查指定数组中符合条件的所有元素。</span><br><span class="line"></span><br><span class="line"> * ```javascript</span><br><span class="line"> //代码实现</span><br><span class="line"> let name = ['hyp','zyp','ypq','ymm']</span><br><span class="line"> let fname= name.filter(function(g){</span><br><span class="line"> return g.indexOf("m")!=-1</span><br><span class="line"> })</span><br><span class="line"> console.log(fname)</span><br></pre></td></tr></table></figure>:hexoPostRenderEscape-->
map()
映射函数 先将数组中每一个元素遍历 元素经过指令变换 生成新的元素 返回新的元素后会将返回的所有元素形成一个新的数组
let name = ['hy','tt','zz'] let newname = name.map(function(t){ return t + 'p' }) console.log(newname) <!--hexoPostRenderEscape:<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">* reduce()</span><br><span class="line"></span><br><span class="line"> * 参数</span><br><span class="line"></span><br><span class="line"> - callback(一个在数组中每一项上调用的函数,接受四个函数:)</span><br><span class="line"> - previousValue(上一次调用回调函数时的返回值,或者初始值)</span><br><span class="line"> - currentValue(当前正在处理的数组元素)</span><br><span class="line"> - currentIndex(当前正在处理的数组元素下标)</span><br><span class="line"> - array(调用reduce()方法的数组)</span><br><span class="line"> - initialValue(可选的初始值。作为第一次调用回调函数时传给previousValue的值)</span><br><span class="line"></span><br><span class="line"> ```javascript</span><br><span class="line"> // 使用reduce方法实现类加求和</span><br><span class="line"> var total = numbers.reduce(function (pre, cur) {</span><br><span class="line"> return pre + cur</span><br><span class="line"> })</span><br><span class="line"> alert(total)</span><br></pre></td></tr></table></figure>:hexoPostRenderEscape-->